#!/usr/bin/env python
# -*- coding: utf-8 -*-

""" Convert numbers written in digits to numbers written in letters according
to the Hungarian grammatical rules.
"""

import sys


DASH_LIMIT = 2000  # az ennél (abszolút értékben) kisebb számokat egybeírjuk

NUMBERS = {(0, 0): "nulla",
           (1, 0): "",
           (3, 0): "száz",
           (4, 0): "ezer",
           (7, 0): "millió",
           (10, 0): "milliárd",  # ezermillió
           (13, 0): "billió",
           (16, 0): "billiárd",  # ezerbillió
           (19, 0): "trillió",
           (22, 0): "trilliárd",  # ezertrillió
           (25, 0): "kvadrillió",
           (28, 0): "kvadrilliárd",  # ezerkvadrillió
           (31, 0): "kvintillió",
           (34, 0): "kvintilliárd",  # ezerkvintillió

           (1, 1): "egy",
           (1, 2): "kettő",
           (1, 3): "három",
           (1, 4): "négy",
           (1, 5): "öt",
           (1, 6): "hat",
           (1, 7): "hét",
           (1, 8): "nyolc",
           (1, 9): "kilenc",

           (2, 10): "tíz",
           (2, 1): "tizen",
           (2, 20): "húsz",
           (2, 2): "huszon",
           (2, 3): "harminc",
           (2, 4): "negyven",
           (2, 5): "ötven",
           (2, 6): "hatvan",
           (2, 7): "hetven",
           (2, 8): "nyolcvan",
           (2, 9): "kilencven"}


def convert_digit_to_word(position, digit):
    """ Convert given digit (number) at position to word.

    >>> convert_digit_to_word(4, 0)
    'ezer'
    >>> convert_digit_to_word(1, 6)
    'hat'

    """
    key = (int(position), int(digit))
    return NUMBERS[key]


def convert_triplet_to_word(number, position, smaller_than_dash_limit):
    """ Convert given triplet (number) at position to word. """
    number_string = ""
    previous_digit = ""
    if number:
        for digit in reversed(str(number)):
            ### a tripleten belüli pozíciót tudjuk meghatározni
            pos_mod_3 = position % 3
            if pos_mod_3 == 0:  # százasok
                number_string = convert_digit_to_word(
                    1, digit) + convert_digit_to_word(3, 0) + number_string
            elif pos_mod_3 == 1:  # egyesek vagy ezresek (ezer és többszörösei)
                sep = "-"
                if smaller_than_dash_limit:
                    sep = ""
                # az egyeseket mindig kiírjuk (önmagukban vagy az ezresek
                # előtt, pl.: egyezer)
                num = convert_digit_to_word(pos_mod_3, digit)
                # egyesek ("") vagy ezresek ("ezer", "millió", ...)
                thousands = convert_digit_to_word(position, 0)
                number_string = num + thousands + sep + number_string
            else:  # tizesek
                dig = digit
                if previous_digit == "0":
                    ### tízről vagy húszról van szó
                    if digit == "1" or digit == "2":
                        dig = digit + "0"
                if digit != "0":
                    number_string = convert_digit_to_word(
                        pos_mod_3, dig) + number_string
            position += 1
            previous_digit = digit

    return number_string


def convert_number_to_word(number):
    """ Convert number written in digits into letters.

    >>> convert_number_to_word(1)
    'egy'
    >>> convert_number_to_word(1078)
    'egyezerhetvennyolc'

    """

    sign = ""
    number_string = ""

    if number:
        if number < 0:
            sign = "mínusz "
            number = -number

        smaller_than_dash_limit = False
        if number < DASH_LIMIT:
            smaller_than_dash_limit = True

        position = 1
        while number > 0:
            # a számot számhármasokra bontjuk, hátulról kezdve (a nullák miatt)
            triplet = number % 1000
            number_string = convert_triplet_to_word(
                triplet, position, smaller_than_dash_limit) + number_string
            position += 3
            number /= 1000
    else:
        number_string = convert_digit_to_word(0, 0)

    number_string = number_string.rstrip("-")
    number_string = sign + number_string

    return number_string


def convert_number(is_interactive=False):
    """ Convert number given on stdin. """
    msg = ""
    if is_interactive:
        msg = "Kérem az egész számot: "

    while True:
        try:
            raw = raw_input(msg)
            number = int(raw)
            number_as_string = convert_number_to_word(number)
            if is_interactive:
                print("%s, azaz %s" % (str(number), number_as_string))
            else:
                print(number_as_string)
        except (EOFError, KeyboardInterrupt):
            return 0
        except (ValueError, KeyError):
            pass


def show_examples():
    """ Print out some examples. """
    for number in range(-21, 122):
        number_as_string = convert_number_to_word(number)
        print("%s, azaz %s" % (str(number), number_as_string))

    for number in range(1978, 2022):
        number_as_string = convert_number_to_word(number)
        print("%s, azaz %s" % (str(number), number_as_string))

    for number in 303030, 303303030, 900000010, 9000000010, \
            90000000010, 900000000010, -123456789012:
        number_as_string = convert_number_to_word(number)
        print("%s, azaz %s" % (str(number), number_as_string))


if __name__ == '__main__':
    # show_examples()
    sys.exit(convert_number(True))
